﻿
namespace App.Data.SqlServer.Repositories
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class CompanyRepository : BaseRepository, ICompanyRepository
    {
        public CompanyRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(Company company)
        {
            this.GetDbSet<Company>().Add(company);
            this.UnitOfWork.SaveChanges();
        }

        public Company GetCompany(Guid companyId)
        {
            return this.GetDbSet<Company>()
                .Where(c => c.Id == companyId)
                .Single();
        }

        public void Update(Company updatedCompany)
        {
            this.GetDbSet<Company>().Attach(updatedCompany);
            this.SetEntityState(updatedCompany, updatedCompany.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid companyId)
        {
            Company companyToDelete = this.GetCompany(companyId);
            this.GetDbSet<Company>().Remove(companyToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Company> GetAll()
        {
            return this.GetDbSet<Company>();
        }
    }
}
